package edu.neepu;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;
import java.sql.SQLOutput;

public class ChapCount {
    String urlSegment = "";
    String chapName = "";
    /**
     *  bookName：中文书名
     *  urlPath：每一本书的路径片段
     */
    public void CountChap(String bookName, String urlPath) {
        String url = "http://www.shicimingju.com/book/" + urlPath + ".html";
        try {
            int count = 0;
            Document document = Jsoup.connect(url).timeout(100000).get();
            Elements chapList = document.select(".book-mulu li");
            for (Element element : chapList) {
                count++;
                Elements aList = element.select("a");
                chapName = aList.text();
                urlSegment = aList.attr("href");
                //爬取每一章节
                //调用对象获得文本
                //Get the download pool and start download.
                DownloadPool pool = DownloadPool.getPool();
                pool.startDownload(urlSegment, bookName, chapName, count); //this will block if no thread available!
//            GetText get = new GetText(urlSegment, bookName, chapName, count);
//            get.getText();
            }
        } catch (IOException e) {
            System.err.println(url + " 爬取失败，错误原因：");
            System.err.println(e.toString());
        }
    }
}
